<?php

namespace app\shop\controller;

use think\Request;
use think\Exception;
use app\shop\model\Customer as CustomerModel;

class Customer extends \PurchaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $customerModel = new CustomerModel();

        try {
            $time = time();
            $data = $customerModel->where([
                'start_time' => ['<=', $time],
                'end_time' => ['>=', $time],
            ])->order('customer_id')->select();

            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $customerModel = new CustomerModel();

        try {

            $this->successTerminate();
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $customerModel = new CustomerModel();

        $data = $request->post();

        /* @var $validate \ShopValidate */
        $validate = \think\Loader::validate('Customer');
        if ( !$validate->scene('')->check($data) ) {
            return $validate->getError();
        }

        try {

            $result = $customerModel->data($data)->allowField(true)->save();

            if (! $result) {
                $this->serverErrorTerminate('资源新增失败');
            }

            $this->successTerminate($customerModel);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $customerModel = new CustomerModel();

        try {
            $data = $customerModel->get($id);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $customerModel = new CustomerModel();

        try {

            $this->successTerminate();
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $customerModel = new CustomerModel();
        $data = $request->post();

        $pk = $customerModel->getPk();
        $id = $data[$pk];
        if ( isset($data[$pk]) ) unset($data[$pk]);

        try {
            $affectedRowCount = $customerModel->allowField(true)->save($data, [
                $pk => $id,
            ]);
            if (! $affectedRowCount) $this->serverErrorTerminate('更新资源失败');
            $this->successTerminate($customerModel);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $customerModel = new CustomerModel();

        try {

            $this->successTerminate();
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
}
